/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/dingdan">dingdan</a> All rights reserved.
 */
package com.thinkgem.dingdan.modules.bu.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.thinkgem.dingdan.common.persistence.Page;
import com.thinkgem.dingdan.common.service.CrudService;
import com.thinkgem.dingdan.modules.bu.dao.BuOrderDao;
import com.thinkgem.dingdan.modules.bu.dao.BuSalesmanDao;
import com.thinkgem.dingdan.modules.bu.entity.BuOrder;
import com.thinkgem.dingdan.modules.bu.entity.BuSalesman;
import com.thinkgem.dingdan.modules.cms.entity.Site;
import com.thinkgem.dingdan.modules.sys.entity.Office;
import com.thinkgem.dingdan.modules.sys.entity.User;
import com.thinkgem.dingdan.modules.sys.utils.UserUtils;

/**
 * 单表生成Service
 * @author ThinkGem
 * @version 2018-09-15
 */
@Service
@Transactional(readOnly = true)
public class BuSalesmanService extends CrudService<BuSalesmanDao, BuSalesman> {

	@Autowired
	private BuOrderDao buOrderDao;
	
	public BuSalesman get(String id) {
		return super.get(id);
	}
	
	public List<BuSalesman> findList(BuSalesman buSalesman) {
		return super.findList(buSalesman);
	}
	
	public Page<BuSalesman> findPage(Page<BuSalesman> page, BuSalesman buSalesman) {
		User user = UserUtils.getUser();
		buSalesman.setOffice(new Office());
		buSalesman.getSqlMap().put("dsf", dataScopeFilter(user, "o", "u"));
		buSalesman.setSite(new Site());
		return super.findPage(page, buSalesman);
	}
	
	@Transactional(readOnly = false)
	public void save(BuSalesman buSalesman) {
		if(!buSalesman.getIsNewRecord()){
			BuOrder buOrder = new BuOrder();
			buOrder.setSalesmanId(buSalesman.getId());
			buOrder.setSalesManName(buSalesman.getSalesmanName());
			buOrderDao.updateSalesNameById(buOrder);
		}
		super.save(buSalesman);
	}
	
	@Transactional(readOnly = false)
	public void delete(BuSalesman buSalesman) {
		super.delete(buSalesman);
	}
	
	
	public BuSalesman findByCustomerId(BuSalesman buSalesman){
		return super.get(buSalesman);
	}
	
}